package com.augurit.wf.common.tag.textarea;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.struts2.ServletActionContext;
import org.springside.modules.utils.SpringContextHolder;

import com.augurit.wf.bi.service.IWfActElemPrivDefService;
import com.augurit.wf.bi.web.form.WfActElemPrivDefForm;
import com.augurit.wf.common.listener.WfInitListener;
import com.augurit.wf.common.tag.base.IWfPageElemWithPriv;
import com.augurit.wf.common.util.WfUtils;


/**
 * textarea 标签
 */
public class WfTextAreaTag extends BodyTagSupport implements IWfPageElemWithPriv{
	private static final long serialVersionUID = 66666L;
	
	private String id;
	private boolean isHiddenByPriv;		//是否是被权限隐藏
	private boolean isDisabledByPriv;	//是否是被权限锁定
	
	private String name;
	private String rows;
	private String cols;
	private String readOnly;
	private String cssClass;
	private String disabled;	//是否锁定
	private String hidden;		//是否隐藏
	
	
	private String debug;
	private String height;
	private String onblur;
	private String onfocus;
	private String width;
	private String align;
	private String style;
	private String size;
	
	private String ondblclick;
	
	/**
	 * 根据页面元素id设置元素权限
	 * @param elementId 页面元素id
	 * @param req
	 */
	public void setPrivByElementId(String elementId, ServletRequest req){
		String templateCode = req.getParameter("templateCode");
		String version = req.getParameter("version");
		String viewName = req.getParameter("viewName");
		String activityName = req.getParameter("activityName");
		
		if(templateCode == null || templateCode.trim().length() == 0 || version == null && viewName == null && viewName.trim().length() == 0){
			throw new RuntimeException("templateCode, version, viewName is null!");
		}
		if(viewName.equals(WfActElemPrivDefForm.DB_ZB_VIEW) && activityName == null){
			throw new RuntimeException("Db、Zb View must be given activityName!");
		}
		
		Map<String, WfActElemPrivDefForm> map = (Map<String, WfActElemPrivDefForm>)ServletActionContext.getServletContext().getAttribute(WfInitListener.PRIV_CACHE);
		if(map == null){
			throw new RuntimeException("Can't get priv control information from cache!");
		}
		
		String key = WfUtils.formatPrivControlKey(elementId, templateCode, version, viewName, activityName);
		
		//从缓存中获取权限信息
		WfActElemPrivDefForm form = map.get(key);
		//缓存中不存在，从数据库中获取
		if(form == null){
			IWfActElemPrivDefService wfActElemPrivDefService = SpringContextHolder.getApplicationContext().getBean(IWfActElemPrivDefService.class);
			form = wfActElemPrivDefService.getElemPriv(templateCode, Integer.parseInt(version), viewName, activityName, elementId);
		}
		
		//从缓存或者数据库中获取到权限信息
		if(form != null){
			this.setHiddenByPriv(form.isHidden());
			this.setDisabledByPriv(form.isLocked());
		}
		//没有该页面元素的权限控制信息
		else{
			WfActElemPrivDefForm virturePrivform = new WfActElemPrivDefForm();
			virturePrivform.setHidden(false);
			virturePrivform.setLocked(false);
			this.setHiddenByPriv(false);
			this.setDisabledByPriv(false);
			map.put(key, virturePrivform);
		}
		
	}
	
	
	public String buildTagStart() {
		StringBuilder str = new StringBuilder();
			 //如果没有被权限隐藏
				
			str.append("<textarea type=\"text\" ");
			if (name != null) {
				str.append(" name=\"" + name + "\"");
			}
			if (this.getId() != null) {
				str.append(" id=\"" + this.getId() + "\"");
			}
				
			if(rows != null){
				str.append(" rows=\"" + rows + "\"");
			}
				
			if(cols != null){
				str.append(" cols=\"" + cols + "\"");
			}

            
            if(this.isDisabledByPriv()){
                str.append(" readonly");
            }
				
			if( cssClass != null){
				str.append(" class=\"" + cssClass + "\"");
			}
			if (height != null) {
				str.append(" height=\"" + height + "\"");
			}
			if (onblur != null) {
				str.append(" onblur=\"" + onblur + "\"");
			}
			if (width != null) {
				str.append(" width=\"" + width + "\"");
			}
			if (align != null) {
				str.append(" align=\"" + align + "\"");
			}
			if (style != null) {
				str.append(" style=\"" + style + "\"");
			}
			if (size != null) {
				str.append(" size=\"" + size + "\"");
			}
				
			if(onfocus != null){
				str.append(" onfocus=\"" + onfocus + "\"");
			}

			if(ondblclick != null){
				str.append(" ondblclick=\"" + ondblclick + "\"");
			}

			str.append(">");
				
		return str.toString();
	}
	
	public String buildTagEnd(){
		return "</textarea>";
	}
	
	public int doAfterBody() {
		try  {
			this.setPrivByElementId(id, this.pageContext.getRequest());
			StringBuilder str = new StringBuilder();
			if(!this.isHiddenByPriv()){
				str.append(this.buildTagStart());
				str.append(this.getBodyContent().getString());
				str.append(this.buildTagEnd());
				
			}
			this.getPreviousOut().print(str.toString());
		}catch(IOException e)  {  
			e.printStackTrace();
		}  
		return SKIP_BODY;  
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getRows() {
		return rows;
	}


	public void setRows(String rows) {
		this.rows = rows;
	}


	public String getCols() {
		return cols;
	}


	public void setCols(String cols) {
		this.cols = cols;
	}


	public String getReadOnly() {
		return readOnly;
	}


	public void setReadOnly(String readOnly) {
		this.readOnly = readOnly;
	}


	public String getCssClass() {
		return cssClass;
	}


	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}


	public String getDisabled() {
		return disabled;
	}


	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}


	public String getHidden() {
		return hidden;
	}


	public void setHidden(String hidden) {
		this.hidden = hidden;
	}


	public String getDebug() {
		return debug;
	}


	public void setDebug(String debug) {
		this.debug = debug;
	}


	public String getHeight() {
		return height;
	}


	public void setHeight(String height) {
		this.height = height;
	}


	public String getOnblur() {
		return onblur;
	}


	public void setOnblur(String onblur) {
		this.onblur = onblur;
	}


	public String getOnfocus() {
		return onfocus;
	}


	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}


	public String getWidth() {
		return width;
	}


	public void setWidth(String width) {
		this.width = width;
	}


	public String getAlign() {
		return align;
	}


	public void setAlign(String align) {
		this.align = align;
	}


	public String getStyle() {
		return style;
	}


	public void setStyle(String style) {
		this.style = style;
	}


	public String getSize() {
		return size;
	}


	public void setSize(String size) {
		this.size = size;
	}


	public boolean isHiddenByPriv() {
		return isHiddenByPriv;
	}


	public void setHiddenByPriv(boolean isHiddenByPriv) {
		this.isHiddenByPriv = isHiddenByPriv;
	}


	public boolean isDisabledByPriv() {
		return isDisabledByPriv;
	}


	public void setDisabledByPriv(boolean isDisabledByPriv) {
		this.isDisabledByPriv = isDisabledByPriv;
	}



	public String getOndblclick() {
		return ondblclick;
	}


	public void setOndblclick(String ondblclick) {
		this.ondblclick = ondblclick;
	}
	

}
